using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using UnityEngine;

public static class NetworkManager
{
    public static Socket socket;
    public static byte[] buffer = new byte[1024];
    public static void Connect()
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(new IPEndPoint(IPAddress.Parse("124.221.109.217"), 10010));
        socket.BeginReceive(buffer, 0, 1024, SocketFlags.None, ReceiveCallback, socket);
    }
    public static void Send(string message)
    {
        byte[] sendBytes = System.Text.Encoding.UTF8.GetBytes(message);
        socket.Send(sendBytes, 0, sendBytes.Length, SocketFlags.None);
    }
    public static void ReceiveCallback(IAsyncResult result)
    {
        Socket socket = (Socket)result.AsyncState;
        int count = socket.EndReceive(result);
        string message = System.Text.Encoding.UTF8.GetString(buffer, 0, count);

        GameManager.actionQueue.Enqueue(() => { UIManager.Instance.contentText.text += "\n" + message; });

        socket.BeginReceive(buffer, 0, 1024, 0, ReceiveCallback, socket);
    }
}
